package org.spring.springboot.app.community.qruserinfo.service.impl;

import org.spring.springboot.app.agent.balancelog.dao.SysOfficeBalanceLogDao;
import org.spring.springboot.app.agent.balancelog.domain.po.SysOfficeBalanceLogPO;
import org.spring.springboot.app.base.User;
import org.spring.springboot.app.community.qruserinfo.dao.QrUserInfoDao;
import org.spring.springboot.app.community.qruserinfo.domain.po.QrUserInfoPO;
import org.spring.springboot.app.community.qruserinfo.domain.vo.QrUserInfoExcelVO;
import org.spring.springboot.app.community.qruserinfo.domain.vo.QrUserInfoSelectVO;
import org.spring.springboot.app.community.qruserinfo.service.IQrUserInfoService;
import org.spring.springboot.app.sys.office.dao.SysOfficeDao;
import org.spring.springboot.app.sys.office.domain.po.SysOfficePO;
import org.spring.springboot.util.IdGen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class QrUserInfoServiceImpl implements IQrUserInfoService {

    @Autowired
    private QrUserInfoDao qrUserInfoDao;
    @Autowired
    private SysOfficeBalanceLogDao sysOfficeBalanceLogDao;

    @Override
    public List<QrUserInfoSelectVO> findPage(int pageNum, int pageSize, String userName, String isFill, String isLoad, String createDateBegin, String createDateEnd, User user) {
        return qrUserInfoDao.findPage(pageNum,pageSize,user.getOfficeId(),userName,isFill,isLoad,createDateBegin,createDateEnd);
    }

    @Override
    public int save(int num,String officeId,String userId,String money) {
        //添加数据
        StringBuilder sql=new StringBuilder("insert into qr_user_info(id,office_id,is_load,create_by,create_date,update_by,update_date,del_flag)VALUES");
        for (int i=0;i<num;i++){
            sql.append("('");
            sql.append(IdGen.uuid());
            sql.append("','");
            sql.append(officeId);
            sql.append("',");
            sql.append("0");
            sql.append(",'");
            sql.append(userId);
            sql.append("',");
            sql.append("now()");
            sql.append(",'");
            sql.append(userId);
            sql.append("',");
            sql.append("now()");
            sql.append(",0),");
        }
        String sqlR=sql.toString();
        sqlR=sqlR.substring(0,sqlR.length()-1);
        qrUserInfoDao.excSql(sqlR);
        //修改金额
        qrUserInfoDao.updateOfficeBalance(officeId,money);
        //记录日志
        SysOfficeBalanceLogPO log=new SysOfficeBalanceLogPO();
        log.preInsert();
        log.setOfficeId(officeId);
        log.setMoney(new BigDecimal(money));
        log.setType(108);
        sysOfficeBalanceLogDao.insertSelective(log);
        return 0;
    }

    @Override
    public int updateBySelect(String state, String officeId, String userName, String isFill, String isLoad, String createDateBegin, String createDateEnd) {
        return qrUserInfoDao.updateBySelect(state,officeId,userName,isFill,isLoad,createDateBegin,createDateEnd);
    }

    @Override
    public List<QrUserInfoExcelVO> findBySelect(String userName, String isFill, String isLoad, String createDateBegin, String createDateEnd, User user) {
        return qrUserInfoDao.findBySelect(user.getOfficeId(), userName, isFill, isLoad, createDateBegin, createDateEnd);
    }
}
